package com.zlx.rulemk.datagen;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.zlx.rulemk.marketing.constant.ConfigNames;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;


/**
 * @Author: zhanglingxing
 * @CreateTime: 2022-07-06
 * @Description: 检测所有环境是否可以正常运行
 * @Version: 1.0
 */
@Slf4j
public class _01_CheckEnvironment {

    // 获取 resource/appliction.properties配置文件
    static Config config = ConfigFactory.load();

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        String host = config.getString(ConfigNames.REDIS_HOST);
        int port = config.getInt(ConfigNames.REDIS_PORT);
        Jedis jedis = new Jedis(host, port);
        String ping = jedis.ping();
        if(StringUtils.isNotBlank(ping)){
            log.warn("redis environment result----> successfully");
        }else{
            log.warn("redis environment result----> failed");
        }

        org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum",config.getString(ConfigNames.HBASE_ZK_QUORUM));
        try {
            Connection hbaseConn = ConnectionFactory.createConnection(conf);
            log.warn("Hbase environment result----> successfully");
        } catch (IOException e) {
            log.warn("Hbase environment result----> failed");
            e.printStackTrace();
        }

        String ckDriver = config.getString(ConfigNames.CK_JDBC_DRIVER);
        String ckUrl = config.getString(ConfigNames.CK_JDBC_URL);
        Class.forName(ckDriver);
        try {
            java.sql.Connection conn = DriverManager.getConnection(ckUrl);
            log.warn("CK environment result----> successfully");
        } catch (SQLException throwables) {
            log.warn("CK environment result----> failed");
            throw new SQLException(throwables);
        }



    }
}
